!--------------------------------------------------------------------
!--------------------------------------------------------------------
subroutine cup_kbcon_catt(cap_inc,iloop, k22, kbcon, he_cup, hes_cup, &
     hkb,kzi,mix, mgmxp, mkx, mgmzp, istart, iend, ierr, kbmax, p_cup,&
     cap_max,j)
  implicit none
  integer i,j,k,kp, mix, mgmxp, mkx, mgmzp, istart, iend, iloop
  integer kbcon(mgmxp), k22(mgmxp), ierr(mgmxp), kbmax(mgmxp),kzi(mgmxp)
  real he_cup(mgmxp,mgmzp), hes_cup(mgmxp,mgmzp), p_cup(mgmxp,mgmzp)
  real pbcdif, cap_max(mgmxp),plus,cap_inc(mgmxp), hkb(mgmxp),hkbpbl
!  real pbcdif, cap_max(mgmxp),plus,cap_inc, hkb(mgmxp),hkbpbl

  !--- Determine the level of convective cloud base  - KBCON
  ! kbcon  = LFC of parcel from k22
  ! k22    = updraft originating level
  ! hkb    = moist static energy at originating level

  do I=ISTART,IEND

   !srf-print---------------------
    !if(j/=16 ) goto 103
    ! if(i /=95) goto 103
   !srf-print---------------------


     !DO 27 I=ISTART,IEND
     kbcon(i)=1
     !IF (ierr(I).NE.0) GO TO 27
     if (ierr(I).ne.0) cycle
     KBCON(I)=K22(I)

     !srf-print---------------------
     	  !if(j==16) then
	    !if(i > 61 .and. i<120) then 
	    !if(i == 95) then 
	    ! print*,'-------------------cap_max(i)------',int(cap_max(i))
     	    ! print*,'1',i,ierr(I),k22(i),kbcon(i),KBMAX(i)
     	     !do k=mkx,k22(i),-1
     	    ! do k=mkx-10,1,-1
     	    ! print*,k,HE_cup(I,K),HES_cup(I,K),p_cup(i,k)
     	    ! enddo
	    !endif
	   !endif
     !srf-print---------------------
     
     GO TO 32
31   continue
     KBCON(I)=KBCON(I)+1
     
     !print*,'---kbcon------',KBCON(I)
     
     if (KBCON(I).gt.KBMAX(i)+2) then
         if(iloop.lt.4)ierr(i)=3
         if(iloop.eq.4)ierr(i)=997
        !GO TO 27

     !srf-print---------------------
     !	  if(j==16) then
     !	    !if(i > 61 .and. i<120) then 
	!    if(i == 95) then 
	!     print*,'-------------------cap_max(i)------',int(cap_max(i))
     	!     print*,'2',i,ierr(I),k22(i),kbcon(i),KBMAX(i)
     	!     !do k=mkx,k22(i),-1
     	!     do k=mkx-10,1,-1
     	!     print*,k,HE_cup(I,K),HES_cup(I,K),p_cup(i,k)
     	!     enddo
	!    endif
	!   endif
     !srf-print---------------------



        cycle
     endif
32   continue

! para uso no futuro
! media de HKB dento da CLP
!       hkbpbl=0.
!       if(k22(i).le.kzi(i))then
!
!	  k22(i)=kzi(i)
!
!	 do kp=1,kzi(i)
!	   hkbpbl=hkbpbl+he_cup(i,kp)
!	 enddo
!	 hkbpbl=hkbpbl/float(kzi(i))
!       else
!	  hkbpbl=he_cup(i,k22(i))
!       endif
!       hkb(i)=hkbpbl
!       print *,'hkbpbl = ',hkbpbl,HE_cup(I,K22(I)),kzi(i)
!
     if (HE_cup(I,K22(I)).lt.HES_cup(I,KBCON(I))) then
!orig     if (HE_cup(I,K22(I)).lt.HES_cup(I,KBCON(I))) GO TO 31
!para uso no futuro
!orig       if(hkbpbl .lt. HES_cup(I,KBCON(I)) )GO TO 31
!       if(hkbpbl .lt. HES_cup(I,KBCON(I)) ) then
       
     !srf-print---------------------
     ! if(j==16 .and. i == 95) then
     !  print*,'-HES <',K22(I),KBCON(I),HE_cup(I,K22(I)),HES_cup(I,KBCON(I))
     ! endif
     !srf-print---------------------
      
      GO TO 31
     else

     !srf-print---------------------
     !  if(j==16 .and. i == 95) then
     !    print*,'-HES>',K22(I),KBCON(I),HE_cup(I,K22(I)),HES_cup(I,KBCON(I))
     ! endif     
     !srf-print---------------------

    endif

     !     Cloud base pressure and max moist static energy pressure
     !     i.e., the depth (in mb) of the layer of negative buoyancy

     if (KBCON(I)-K22(I).eq.1) then
        !GO TO 27
        cycle
     endif

     PBCDIF = -P_cup(I,KBCON(I)) + P_cup(I,K22(I))
!     plus=cap_max(i)-float(iloop-1)*cap_inc 
     plus=cap_max(i)-float(iloop-1)*cap_inc(i)
!srf-out2004    
!     plus=max(25.,cap_max(i)-float(iloop-1)*cap_inc)
     plus=max(25.,cap_max(i)-float(iloop-1)*cap_inc(i))

     !srf-print---------------------
     !  if(j==16 .and. i == 95) then
     !    print*,'---------'
     !    print*,'1 plus PBCDIF',K22(I),KBCON(I), plus,PBCDIF
     ! endif     
     !srf-print---------------------
     
!    if (PBCDIF.gt.cap_max(i)) then
     if (PBCDIF.gt.plus) then
        K22(I)   = K22(I)+1
        KBCON(I) = K22(I)
     
     !srf-print---------------------
     !  if(j==16 .and. i == 95) then
     !     print*,'2 K22 KBCON', k22(i),kbcon(i)
     !     print*,'---------'
     !  endif     
     !srf-print---------------------
     
    
        GO TO 32
     endif
     !27   CONTINUE

!srf-print---------------------
!    103 continue
!     	  if(j==16 .and. i==95) stop 4443
!srf-print---------------------


  enddo

!srf-print---------------------
!    	  if(j==16) stop 4443
!srf-print---------------------



  return
end subroutine cup_kbcon_catt

!-------------------------------------------------------------------

subroutine get_zi(mix,mgmxp,mkx,mgmzp,istart,iend,j,ierr,kzi,tkeg, &
                  rcpg,z,ztop,tkmin)

  implicit none
  integer mix,mgmxp,mkx,mgmzp,i,k,istart,iend,j,kzimax,ktke_max
  real tkmin,tke_tmp
  real,    dimension(mgmxp,mgmzp) :: tkeg,rcpg,z
  real,    dimension(mgmxp)	  :: ztop
  integer, dimension(mgmxp)	  :: kzi,ierr

  real, parameter :: rcpmin = 1.e-5, pblhmax = 3000. 
  !print*,j,mgmxp,mgmzp,mix,istart,iend
  
  do i=istart,iend
    kzi(i)  = 2

    if(ierr(i).eq.0)then
     tke_tmp = 0.
     ktke_max= 1
     !---  max level for kzi
     do k=1,mkx
       if(z(i,k).ge. pblhmax+ztop(i)) then	
          kzimax = k
          !print*,z(i,k), pblhmax,ztop(i),kzimax
          exit
       endif
     enddo
     !---
     !       go to 201
     !level of max tke  below kzimax and w/out clouds
     do  k=1,kzimax
       !print*,k,tkeg(i,k), tke_tmp,ktke_max,kzimax
       if(rcpg(i,k) .lt. rcpmin) then
	 if( tkeg(i,k) .ge. tke_tmp) then
	   tke_tmp = tkeg(i,k)
	   cycle
	 else
	   ktke_max= max(1,k-1)
	   exit
	 endif
       endif	   
     enddo	     
     !201    continue
!         print*,ktke_max

     do k=ktke_max,kzimax+1
!  	print*,rcpg(i,k),tkeg(i,k),k,kzi(i),i
	if(rcpg(i,k) .lt. rcpmin) then
          if(tkeg(i,k) .gt. 1.1*tkmin)  then
	    kzi(i) = k
	    cycle
	  endif
        else
	   kzi(i) = k
	   exit
	endif
     enddo
     kzi(i) = max(2     ,kzi(i))
     kzi(i) = min(kzimax,kzi(i))
     !print*,'ktke_max kzi kzimax =',ktke_max,kzi(i),kzimax
     !if(ktke_max.gt.kzi(i)) then
     !print*,'ktke_max > kzi:', ktke_max,kzi(i),i,j
     !do k=1,kzimax
     !print*,k,tkeg(i,k),rcpg(i,k),tkmin
     !enddo
     !stop
     !endif

     !if(kzi(i) .gt. 15 ) then
     !print*,'j i KZI=',j,i,kzi(i)
     !do k=1,mkx
     !print*,k,tkeg(i,k),rcpg(i,k),tkmin
     !enddo
     !stop 1222
     !endif
     !print*,kzi(i)
     !stop

   endif
 enddo
end subroutine get_zi
